package com.sh.suhe.utildemos.views.activity.draw;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

import com.sh.suhe.utildemos.R;
import com.sh.suhe.utildemos.utils.LogUtil;

public class PathDrawActivity extends AppCompatActivity {
    private String TAG = PathDrawActivity.class.getSimpleName();
    int mShadowColor,bgColor;
    private Paint mPaintShadow;
    TextureView textureView;
    Surface mDrawingSurface;
    Path mShadowPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_path_draw);

        textureView = findViewById(R.id.path_tv);


        Resources resources = this.getResources();
        mShadowColor = resources.getColor(R.color.stock_time_chart_shadow);
        bgColor = resources.getColor(R.color.colorAccent);
        init();
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Surface drawingSurface = new Surface(surface);
                mDrawingSurface = drawingSurface;

            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
    }
    public void init(){
        mPaintShadow = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintShadow.setColor(mShadowColor);
        mPaintShadow.setStyle(Paint.Style.FILL);
        mShadowPath = new Path();

    }

    public void startDraw(View view) {
        float x = textureView.getX();
        float y = textureView.getY();
        int height = textureView.getLayoutParams().height;
        LogUtil.d(TAG,"startDraw  y:"+y +" height:"+ height);
        int lineY =  (int) (height - 60);
        Canvas canvas = null;
        try {
            canvas = mDrawingSurface.lockCanvas(/*mSurfaceRect*/null);
            if (canvas == null) {
                return;
            }
            canvas.drawColor(bgColor);
            mShadowPath.reset();
            mShadowPath.moveTo(20, lineY);
            mShadowPath.lineTo(110,100);
            mShadowPath.lineTo(220,90);
            mShadowPath.lineTo(330,310);
            mShadowPath.lineTo(440,160);
            mShadowPath.lineTo(550,200);
            mShadowPath.lineTo(660,20);
            mShadowPath.lineTo(770,150);

            mShadowPath.lineTo(770,lineY);
            mShadowPath.close();
            canvas.drawPath(mShadowPath, mPaintShadow);
            LogUtil.d(TAG,"onSurfaceTextureAvailable end ");
            mDrawingSurface.unlockCanvasAndPost(canvas);
        }catch (Exception e){

        }
    }
}
